/**
 * @description: 文件描述
 * @version: 0.0.1
 * @author: xxx
 * @Date: 2025-02-27 09:57:16
 */
import { createRouter, createWebHistory } from 'vue-router'
import NotFoundView from '../pages/NotFoundView.vue'
import Layout from '../Layout/Layout.vue'
import LoginView from '../pages/LoginView.vue'
import VideoConig from '../pages/VideoConigView.vue'
import QrConfigView from '../pages/QrConfigView.vue'
import { useStore } from '@/store/useState'
import { HomeFilled } from "@element-plus/icons-vue"; // 添加图标导入
const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [  // 定义子路由
      {
        path: 'home',
        component: VideoConig,
        name: 'home',
        meta: {
          title: '视频配置',
          icon: HomeFilled,
          showInMenu: true, // 标记在菜单中显示
          menuOrder: 1
        }
      },
      {
        path: 'about',
        component: QrConfigView,
        name: 'about',
        meta: {
          title: '二维码配置',
          icon: HomeFilled,
          showInMenu: true,
          menuOrder: 2
        }
      },
    ]
  },
  {
    path: '/login',
    component: LoginView,
    name: 'login',
  },

  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFoundView
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_BASE_ROUTER),
  routes,
})

router.beforeEach(async (to, from, next) => {
  const userStore = useStore()
  if (to.path == '/login') {
    next();
  } else if (userStore.userInfo.token) {
    next();
    // // 每次进入都需要重新登录
    // if(from.path == '/'){
    //   userStore.deleteToken()
    //   next('/login');
    // }else{
    //   next();
    // }
  } else {
    next('/login');
  }
})

export default router